export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },

      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus) => {
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/user/login", "POST", params);
};

// 注册
export const register = (params) => {
  return ajaxRequest("/user/register", "POST", params);
};

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/${id}`, "POST", params);
};

// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/users", "POST", params);
};

// 获取用户列表
export const getUser = (params) => {
  return ajaxRequest("/users", "GET", params);
};

// getUserInfo
export const getUserInfo = (id) => {
  return ajaxRequest(`/user/${id}`, "GET");
};

export const hasAdmin = (params) => {
  return ajaxRequest(`/users/hasAdmin`, "POST", params);
}

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/${id}`, "DELETE");
}


// staff 相关
export const getAllStaff = (params) => {
  return ajaxRequest(`/staff/getAllStaff`, "GET", params);
}

export const getAllStaffPost = (params) => {
  return ajaxRequest(`/staff/getAllStaff`, "POST", params);
}


export const addStaff = (params) => {
  return ajaxRequest(`/staff/addStaff`, "POST", params);
}

export const updateStaff = (id, params) => {
  return ajaxRequest(`/staff/updateStaff/${id}`, "POST", params);
}

export const deleteStaff = (id) => {
  return ajaxRequest(`/staff/delete/${id}`, "DELETE");
}

// getAllBackup
export const getAllBackup = (params) => {
  return ajaxRequest(`/backup/getAllBackup`, "GET", params);
}
export const addBackup = (params) => {
  return ajaxRequest(`/backup/addBackup`, "POST", params);
}

export const getTransport = (params) => {
  return ajaxRequest(`/transport/get`, "POST", params);
}

export const getWasteInfo = (params) => {
  return ajaxRequest(`/process/getWasteInfo`, "POST", params);
}
export const getReceiveInfo = (params) => {
  return ajaxRequest(`/process/getReceiveInfo`, "POST", params);
}

export const getStageInfo = (params) => {
  return ajaxRequest(`/process/getStageInfo`, "POST", params);
}
export const addStageInfo = (params) => {
  return ajaxRequest(`/process/addStageInfo`, "POST", params);
}
export const updateStageInfo = (id, params) => {
  return ajaxRequest(`/process/updateStageInfo/${id}`, "POST", params);
}
export const addWasteInfo = (params) => {
  return ajaxRequest(`/process/addWasteInfo`, "POST", params);
}
export const addTransInfo = (params) => {
  return ajaxRequest(`/process/addTransInfo`, "POST", params);
}
export const addHandle = (params) => {
  return ajaxRequest(`/process/addHandle`, "POST", params);
}
export const getPackageTimeline = (params) => {
  return ajaxRequest(`/process/getPackageTimeline`, "POST", params);
}
export const getWeightAbmormal = (params) => {
  return ajaxRequest(`/process/getWeightAbmormal`, "GET", params);
}
export const updateWasteInfo = (id, params) => {
  return ajaxRequest(`/process/updateWasteInfo/${id}`, "POST", params);
}


